package com.ohos.carbon.slice.libraries;

import com.ohos.carbon.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;

public class PictureSelectAbilitySlice extends AbilitySlice implements ListItemProvider.OnItemClickListener {
    private ListContainer listContainer;
    private ListItemProvider listItemProvider;
    private ArrayList<PictureMo> pictures = new ArrayList<>();

    public PictureSelectAbilitySlice() {
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_picture_select);
        initLayout();
        initPicture();
    }

    private void initPicture() {
        pictures.add(new PictureMo(ResourceTable.Media_selpic_bg1, "resource/media/bg1.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_bg2, "resource/media/bg2.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_cloudy, "resource/media/cloudy.png"));
        pictures.add(new PictureMo(ResourceTable.Media_icon, "resource/media/icon.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_overcast, "resource/media/overcast.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_rain, "resource/media/rain.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_snow, "resource/media/snow.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_sunshine, "resource/media/sunshine.png"));
        pictures.add(new PictureMo(ResourceTable.Media_selpic_thunder, "resource/media/thunder.png"));
        listItemProvider.setData(pictures);
    }


    private void initLayout(){
        listContainer = (ListContainer)findComponentById(ResourceTable.Id_list_view);
        listItemProvider = new ListItemProvider(this, this);
        listContainer.setItemProvider(listItemProvider);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void OnItemClick(PictureMo pictureMo, int position) {
        Intent intent_picture_select = new Intent();
        intent_picture_select.setParam("picture_res_id",  pictureMo.pictureId);
        setResult(intent_picture_select);
        terminate();
    }
}
